package com.zy.explore.demo.application.service;

import com.zy.explore.demo.domain.entity.SysUser;
import com.zy.explore.demo.interfaces.assembler.SysUserAssembler;
import com.zy.explore.demo.interfaces.frm.SysUserFrm;
import com.zy.explore.demo.interfaces.vo.SysUserVO;

import com.zy.explore.frm.CommonIdIntFrm;
import com.zy.explore.vo.PageVO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;


@Service
public class SysUserService {

    /**
     * 新增数据
     *
     * @param frm 待新增的frm对象
     * @return 成功返回新增后的VO对象，失败返回null
     */
    public SysUserVO insert(SysUserFrm frm) {
        SysUser entity = SysUserAssembler.convertFromFrm(frm);
        entity.save();
        return SysUserAssembler.convertToVO(entity);
    }

    /**
     * 删除数据
     *
     * @param frm 主键frm对象
     */
    public Integer delete(CommonIdIntFrm frm) {
        SysUser entity = SysUser.builder().id(frm.getId()).build();
        entity.remove();
        return 1;
    }

    /**
     * 更新数据
     *
     * @param frm 待更新的frm对象
     * @return 成功返回更新后的VO对象，失败返回null
     */
    public SysUserVO update(SysUserFrm frm) {
        SysUser entity = SysUserAssembler.convertFromFrm(frm);
        entity.updateSelective();
        return SysUserAssembler.convertToVO(entity);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param frm 主键Frm对象
     * @return 成功返回对应ID的VO对象，失败返回null
     */
    public SysUserVO getById(CommonIdIntFrm frm) {
        SysUser entity = SysUser.builder().id(frm.getId()).build();
        return SysUserAssembler.convertToVO(entity.getById());
    }

    /**
     * 根据条件获取数据集
     *
     * @param frm 查询条件对象
     * @return 成功返回满足条件的数据集合，失败返回空集合
     */
    public List<SysUserVO> getList(SysUserFrm frm) {
        List<SysUserVO> retList = new ArrayList<>();
        // 转换查询条件
        Specification<SysUser> specification = SysUserAssembler.convertToSpecification(frm);
        // 转换排序条件
        Sort sort = SysUserAssembler.getSort(frm);
        SysUser entity = SysUser.builder().build();
        List<SysUser> list = entity.findAll(specification, sort);
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(x -> retList.add(SysUserAssembler.convertToVO(x)));
        }
        return retList;

    }

    /**
     * 根据条件查询数据分页
     *
     * @param frm 查询条件参数
     * @return 成功返回满足条件的分页，失败返回空页
     */
    public PageVO<SysUserVO> getPageInfo(SysUserFrm frm) {
        // 转换查询条件
        Specification<SysUser> specification = SysUserAssembler.convertToSpecification(frm);
        // 转换排序条件
        Sort sort = SysUserAssembler.getSort(frm);
        SysUser entity = SysUser.builder().build();
        Page<SysUser> page = entity.findAll(specification, frm.getPageNum(), frm.getPageSize(), sort);
        Page<SysUserVO> retPage = new PageImpl<>(new ArrayList<>(), page.getPageable(), page.getTotalElements());
        if (page.hasContent()) {
            List<SysUserVO> voList = new ArrayList<>();
            page.getContent().forEach(x -> voList.add(SysUserAssembler.convertToVO(x)));
            retPage = new PageImpl<>(voList, page.getPageable(), page.getTotalElements());
        }
        return new PageVO<>(retPage);
    }
}